package com.exartisan.thanhdoanhn;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.exartisan.adapter.ForumCategoryAdapter;
import com.exartisan.bean.ForumCategory;
import com.exartisan.network.NetworkUtility;
import com.exartisan.network.RestConnector;
import com.exartisan.network.StupidResponseTranslater;
import com.exartisan.network.WeirdParametersBuilder;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class ListCategoryForumActivity extends BaseActivity
{
	ForumCategoryAdapter adapter;
	ListView listView;
	ArrayList<ForumCategory> categories;
	ProgressDialog progressDialog;
	
	@Override
	protected void setUpActionBar() {
		super.setUpActionBar();
		getSupportActionBar().hide();
	}

	@Override
	protected void initUI()
	{
		setContentView(R.layout.forum_main);
		listView = (ListView)findViewById(R.id.lv_mainForum);
	}

	@Override
	protected void initProcess()
	{
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				Intent i = new Intent(getBaseContext(), ListAcivity.class);
				i.putExtra("categoryId", categories.get(pos).getCategoryId());
				i.putExtra("type", 1);
				i.putExtra("postType", 2);
				startActivity(i);
			}
		});
		doNetWorkTask(WeirdParametersBuilder.BuildParams(WeirdParametersBuilder.BuildCategoryForumData()));
	
		/*final ArrayList<ForumCategory> listCate = new ArrayList<ForumCategory>();
		for(int i=0; i<20; i++){
			ForumCategory catalogy =new ForumCategory();
			catalogy.setTitle("Context" +i);
			catalogy.setIcon("http://xemanhdep.com/wp-content/uploads/2013/04/hinh-anh-avatar-dep.jpg");
			
			listCate.add(catalogy);
			
		}
		ForumCategoryAdapter adapter=new ForumCategoryAdapter(this,1,listCate);
		listView.setAdapter(adapter);*/
		
	}
	
	private void doNetWorkTask(RequestParams requestParams) {
		if (NetworkUtility.checkNetworkState(getApplicationContext())) {
			progressDialog = ProgressDialog.show(getActivity(), "Đang kết nối", "Đang tải dữ liệu");
			RestConnector.post("", requestParams,
					new AsyncHttpResponseHandler() {
						@Override
						public void onSuccess(int arg0, String response) {
							progressDialog.dismiss();
							categories = StupidResponseTranslater.getCategoryForum(response);
							adapter = new ForumCategoryAdapter(getBaseContext(), 1, categories);
							listView.setAdapter(adapter);
							super.onSuccess(arg0, response);
						}

						@Override
						public void onFailure(Throwable arg0, String arg1) {
							super.onFailure(arg0, arg1);
							Toast.makeText(getBaseContext(), "Không thể kết nối", Toast.LENGTH_SHORT).show();
							finish();
						}
					});
		}
	}
	
	public void onClickBack(View v){
		finish();
	}

}
